<template>
	<view class="container">
		<view class="logo">
			<image src="../../static/img/index/linghuo.png" alt="" />
		</view>
		<view class="platform_name">职小侠</view>
		<button class="login_btn" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">微信登录</button>
	</view>
</template>

<script>
	const Page = require('../../utils/mtj-wx-sdk.js').Page;
	import {
		getToken
	} from "../../api/user.js"
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				code: "",
				SessionKey: '',
				encryptedData: "",
				iv: "",
				OpenId: '',
				user_id:'',
				nickName: null,
				avatarUrl: null,
				isCanUse: uni.getStorageSync('isCanUse'), //默认为true  记录当前用户是否是第一次授权使用的				
				isInvite: false,
				job_id: ''
			}
		},
		onLoad(e) {
			if (e.id) {
				this.job_id = e.id
				this.user_id = e.user_id
				this.isInvite = true
			} else {
				this.isInvite = false
			}
		},
		methods: {
			...mapMutations(['login', "getUserInfo", "avatar", "username"]),
			wxGetUserInfo() { //第一授权获取用户信息===》按钮触发
				this.loginin()
				let _this = this;

			},
			goNext() {
				if (this.isInvite) {
					let data = {
						recruit_job_id: this.job_id,
						user_id:this.user_id
					}
					data = JSON.stringify(data)
					uni.navigateTo({
						url: '/pages/detail/detail?data=' + data
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}
			},
			loginin() {
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				});

				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						_this.code = loginRes.code;
						if (!_this.isCanUse) {
							//非第一次授权获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: infoRes=> {
									//获取用户信息后向调用信息更新方法
									_this.nickName = infoRes.userInfo.nickName; //昵称
									_this.avatarUrl = infoRes.userInfo.avatarUrl; //头像
									_this.avatar({
										data: _this.avatarUrl,
										cb: () => {}
									});
									_this.username({
										data: _this.nickName,
										cb: () => {}
									});
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: (infoRes) => {
											uni.setStorageSync('isCanUse', false); //记录是否第一次授权 false:表示不是第一次授权
											_this.encryptedData = infoRes.encryptedData
											_this.iv = infoRes.iv
											_this.nickName = infoRes.userInfo.nickName; //昵称
											_this.avatarUrl = infoRes.userInfo.avatarUrl; //头像
											// console.log(_this.code)
											// return
											getToken({
												username: _this.nickName,
												code: _this.code,
												avatar:_this.avatarUrl
											}).then(res => {
												if (res.code == 20000) {
													_this.login({
														data: res.data.token,
														cb: () => this.goNext()
													});
												} else {
													uni.showModal({
														title: "授权失败",
														content: '用户已登出，请重新登录！',
														success: (res) => {
															if (res.confirm) {
																uni.navigateTo({
																	url: "/pages/login/index"
																});
															}
														}
													})
												}
											})
										}
									});

								}
							});
						}
						// 将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						uni.hideLoading();
					},
				})
			},
		}

	}
</script>

<style scoped>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.logo {
		width: 100px;
		height: 100px;
		margin-top: 80px;
	}
	.logo image{
		width: 100px;
		height: 100px;
	}
	.platform_name {
		font-size: 26px;
		font-weight: blod;
		color: #333333;
		margin: 30px 0;
	}

	.login_btn {
		width: 80%;
		height: 50px;
		text-align: center;
		line-height: 50px;
		color: #FFFFFF;
		background-color: #0AC770;
		font-size: 18px;
		border-radius: 27px;
	}
</style>
